package com.lanhu.designpattern.prototype.deep;

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.io.*;
import java.util.List;

/**
 * author:zhangzg
 * date:2020/11/8 18:17
 */
public class WordDocument implements Cloneable, Serializable{

    private String title;

    private String name;

    private List<String> document;

    public WordDocument(String title, String name) {
        this.title = title;
        this.name = name;
    }

    public WordDocument(String title, String name, List<String> document) {
        this.title = title;
        this.name = name;
        this.document = document;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    protected WordDocument clone(){
        return this.deepClone();
    }

    public List<String> getDocument() {
        return document;
    }

    public void setDocument(List<String> document) {
        this.document = document;
    }

    @Override
    public String toString() {
        return "title = " + title + ",name = " + name;
    }

    public WordDocument deepClone(){
        //字节数组输出流创建缓冲区
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            //对象输出流进行写入数据
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(this);
            //字节数组输入流缓冲区
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            WordDocument object = (WordDocument)objectInputStream.readObject();
            return object;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return null;

    }
}
